﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Data;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CommonMethodServiceRef;

    public class SqlQuery : BasePage
    {
        protected Button btnGoBack;
        protected Button btnSearch;
        protected HtmlForm form1;
        protected GridView gvResult;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected TextBox txtSql;

        private void BindData2GridView(BaseQuery baseQuery)
        {
            if (!string.IsNullOrEmpty(this.strSQL))
            {
                DataSet ds = CommoneMethodServiceClientProxy.GetInstance().SqlQuery(this.strSQL, baseQuery);
                this.gvResult.DataSource = ds;
                this.gvResult.DataBind();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                string sSQL = this.txtSql.Text.Trim().ToUpper();
                if ((!sSQL.Contains("INSERT") && !sSQL.Contains("UPDATE")) && !sSQL.Contains("DELETE"))
                {
                    BaseQuery baseQuery = new BaseQuery {
                        IsGetAll = true,
                        CurrentPage = "0",
                        PageSize = this.gvResult.PageSize.ToString(),
                        SortField = "",
                        SortDirection = "DESC"
                    };
                    this.BindData2GridView(baseQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void gvResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                this.txtSql.Text = "";
            }
        }

        public string strSQL
        {
            get
            {
                return (!string.IsNullOrEmpty(this.txtSql.Text.Trim()) ? this.txtSql.Text.Trim() : "");
            }
        }
    }
}

